package com.ibsol.control
{
	import com.ibsol.model.SystemEnv;
	import com.ibsol.model.User;
	import com.ibsol.service.UserService;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	
	import org.alivepdf.colors.RGBColor;
	import org.alivepdf.display.Display;
	import org.alivepdf.drawing.Blend;
	import org.alivepdf.drawing.Caps;
	import org.alivepdf.drawing.Joint;
	import org.alivepdf.fonts.FontFamily;
	import org.alivepdf.fonts.Style;
	import org.alivepdf.layout.Layout;
	import org.alivepdf.layout.Orientation;
	import org.alivepdf.layout.Size;
	import org.alivepdf.layout.Unit;
	import org.alivepdf.pdf.PDF;
	import org.alivepdf.saving.Method;;

	
	public class PrintBookPDF
	{
		public var user:User = new User();
		public var currPackageName:String = "";
		public var billAC:ArrayCollection = new ArrayCollection();
		public var userAC:ArrayCollection = new ArrayCollection();
		public var subtotal:String = "";
		public var grandtotal:String = "";
		public var discount:String = "";
		public var tax:String = "";
		public var sysEnv:SystemEnv;
		
		public function PrintBookPDF()
		{
			sysEnv = SystemManager.getSystemEnv("tourix");
			var userService:UserService = new UserService();
			userAC = userService.getUserListBySystemId(sysEnv.uid);
		}
		
		public function printPDF():void
		{
			var myPDF:PDF = new PDF(Orientation.PORTRAIT, Unit.CM, Size.A4 ); 
			var myDate:Date = new Date();
			var cutY:int=105;
			//PDF Banner and Text Contents
			myPDF.setDisplayMode(Display.FULL_PAGE,Layout.SINGLE_PAGE);
			myPDF.addPage();
			myPDF.lineStyle(new RGBColor(1,178,255), 0, 1, Blend.NORMAL, null, Caps.NONE, Joint.BEVEL );
			myPDF.beginFill(new RGBColor(1,178,255));
			myPDF.drawRect (new Rectangle( 5, 5, 200, 14 ));
			myPDF.textStyle(new RGBColor(255,255,255),1);
			myPDF.setFont( FontFamily.HELVETICA, Style.ITALIC, 12 );
			myPDF.setFontSize(13);
			myPDF.addText('Travel Agent Banner',10,12);
			myPDF.lineStyle(new RGBColor(255,255,255),0,1, Blend.NORMAL,null,Caps.NONE,Joint.BEVEL);//设定标题
			myPDF.setXY(5,20);
			myPDF.setFontSize(12);
			myPDF.textStyle(new RGBColor(0,0,0),1);//标题下方框里的文字
			myPDF.addCell(200,10,"Dear Customer:",1);
			myPDF.setXY(7,30);
			myPDF.addCell(200,8,"Following is the detailed bill summary of tour package you have selected. If you have any",1);
			myPDF.setXY(7,38);
			myPDF.addCell(200,8,"inquiries,please feel free to contact our sales executive.",1);
			myPDF.setXY(7,46);
			myPDF.addCell(200,8,"Sales Executive:",1);
			myPDF.setXY(10,54);
			myPDF.addCell(190,6, userAC[0].acc);
			myPDF.setXY(10,60);
			myPDF.addCell(190,6,"Mobile: " + userAC[0].mobile);
			myPDF.setXY(10,66);
			myPDF.addCell(190,6,"Email: " + userAC[0].email);
			//PDF Bill table
			myPDF.beginFill(new RGBColor(1,178,255));
			myPDF.drawRect(new Rectangle(5,80,200,3));		
			myPDF.textStyle(new RGBColor(0,0,0),1);
			myPDF.setXY(150,85);
			myPDF.writeText(13,myDate.toLocaleDateString() + " " + myDate.toLocaleTimeString());
			myPDF.lineStyle(new RGBColor(0,0,0),0,1, Blend.NORMAL,null,Caps.NONE,Joint.BEVEL);
			myPDF.setXY(5,95);
			myPDF.textStyle(new RGBColor(0,0,0),1);
			myPDF.setFont('Arial',Style.NORMAL , 11);
			//Set package name
			myPDF.addMultiCell(200,10,"     "+currPackageName,1);
			myPDF.setXY(5,cutY);
			myPDF.addCell(50,10,"Type",1,0,"C");
			myPDF.setXY(55,cutY);
			myPDF.addCell(40,10,"Price",1,0,"C");
			myPDF.setXY(95,cutY);
			myPDF.addCell(60,10,"Number of Customer",1,0,"C");
			myPDF.setXY(155,cutY);
			myPDF.addCell(50,10,"Amount",1,0,"C");
			myPDF.setFont('Arial',Style.NORMAL,8);
			//设定的表格的第一列  写入Type
			myPDF.setFontSize(11);
			myPDF.setXY(5,115);
			myPDF.addCell(50,10,billAC[0].df_type,1,0,"C");
			myPDF.setXY(5,125);
			myPDF.addCell(50,10,billAC[1].df_type,1,0,"C");
			myPDF.setXY(5,135);
			myPDF.addCell(50,10,billAC[2].df_type,1,0,"C");
			myPDF.setXY(5,145);
			myPDF.addCell(50,10,billAC[3].df_type,1,0,"C");
			myPDF.setXY(5,155);
			myPDF.addCell(50,10,billAC[4].df_type,1,0,"C");
			myPDF.setXY(5,165);
			myPDF.addCell(50,10,billAC[5].df_type,1,0,"C");
			//设定表格的第二列      写入price
			myPDF.setXY(55,115);
			myPDF.addCell(40,10,billAC[0].df_unitPrice,1,0,"C");
			myPDF.setXY(55,125);
			myPDF.addCell(40,10,billAC[1].df_unitPrice,1,0,"C");
			myPDF.setXY(55,135);
			myPDF.addCell(40,10,billAC[2].df_unitPrice,1,0,"C");
			myPDF.setXY(55,145);
			myPDF.addCell(40,10,billAC[3].df_unitPrice,1,0,"C");
			myPDF.setXY(55,155);
			myPDF.addCell(40,10,billAC[4].df_unitPrice,1,0,"C");
			myPDF.setXY(55,165);
			myPDF.addCell(40,10,billAC[5].df_unitPrice,1,0,"C");
			//设定表格的第三列          写入num
			myPDF.setXY(95,115);
			myPDF.addCell(60,10,billAC[0].df_number,1,0,"C");
			myPDF.setXY(95,125);
			myPDF.addCell(60,10,billAC[1].df_number,1,0,"C");
			myPDF.setXY(95,135);
			myPDF.addCell(60,10,billAC[2].df_number,1,0,"C");
			myPDF.setXY(95,145);
			myPDF.addCell(60,10,billAC[3].df_number,1,0,"C");
			myPDF.setXY(95,155);
			myPDF.addCell(60,10,billAC[4].df_number,1,0,"C");
			myPDF.setXY(95,165);
			myPDF.addCell(60,10,billAC[5].df_number,1,0,"C");
			//设定表格的第四列 并且计算出总和
			myPDF.setXY(155,115);
			myPDF.addCell(50,10,billAC[0].df_subtotal,1,0,"C");
			myPDF.setXY(155,125);
			myPDF.addCell(50,10,billAC[1].df_subtotal,1,0,"C");
			myPDF.setXY(155,135);
			myPDF.addCell(50,10,billAC[2].df_subtotal,1,0,"C");
			myPDF.setXY(155,145);
			myPDF.addCell(50,10,billAC[3].df_subtotal,1,0,"C");
			myPDF.setXY(155,155);
			myPDF.addCell(50,10,billAC[4].df_subtotal,1,0,"C");
			myPDF.setXY(155,165);
			myPDF.addCell(50,10,billAC[5].df_subtotal,1,0,"C");
			//Sum
			myPDF.setXY(5,175);
			myPDF.addCell(50,10,"Remarks",1,0,"C");
			myPDF.setXY(55,175);
			myPDF.addCell(150,10,"",1,0,"C");
			//Sum column 1
			myPDF.setXY(5,185);
			myPDF.addCell(50,10,"",1,0,"C");
			myPDF.setXY(5,195);
			myPDF.addCell(50,10,"",1,0,"C");
			myPDF.setXY(5,205);
			myPDF.addCell(50,10,"",1,0,"C");
			myPDF.setXY(5,215);
			myPDF.addCell(50,10,"",1,0,"C");
			//Sum column 2
			myPDF.setXY(55,185);
			myPDF.addCell(40,10,"",1,0,"C");
			myPDF.setXY(55,195);
			myPDF.addCell(40,10,"",1,0,"C");
			myPDF.setXY(55,205);
			myPDF.addCell(40,10,"",1,0,"C");
			myPDF.setXY(55,215);
			myPDF.addCell(40,10,"",1,0,"C");
			//Sum column 3
			myPDF.setXY(95,185);
			myPDF.addCell(60,10,"Sub Total:",1,0,"C");
			myPDF.setXY(95,195);
			myPDF.addCell(60,10,"Tax:",1,0,"C");
			myPDF.setXY(95,205);
			myPDF.addCell(60,10,"Discount:",1,0,"C");
			myPDF.setXY(95,215);
			myPDF.addCell(60,10,"Grand Total",1,0,"C");
			//Sum column 4
			myPDF.setXY(155,185);
            myPDF.addCell(50,10,getSubtotal().toString(),1,0,"C");
            myPDF.setXY(155,195);
            myPDF.addCell(50,10,tax,1,0,"C");
             myPDF.setXY(155,205);
            myPDF.addCell(50,10,discount,1,0,"C");
             myPDF.setXY(155,215);
            myPDF.addCell(50,10,grandtotal,1,0,"C");
			//Save the PDF
			myPDF.end();
			var pdfName:String = myDate.fullYear + "_" + (myDate.month + 1) + "_" + myDate.date + "_" +
								myDate.hours + "_" + myDate.minutes + "_" + myDate.seconds;
			var f:FileStream = new FileStream();
		 	var file:File = File.desktopDirectory.resolvePath("Tourix/" + pdfName + ".pdf");
		 	f.open(file,FileMode.WRITE);
		 	var bytes:ByteArray = myPDF.savePDF(Method.LOCAL);
		 	f.writeBytes(bytes);
		 	f.close();
		 	//Open the PDF using web browser
		 	var webURL:URLRequest = new URLRequest(File.desktopDirectory.nativePath + "/Tourix/" + pdfName + ".pdf");
		 	navigateToURL(webURL, "_blank");
		}
		
		private function getSubtotal():uint
		{
			var sum:uint = 0;
			for(var i:uint=0; i<billAC.length; i++)
			{
				sum += billAC[i].df_subtotal;
			}
			return sum;
		}

	}
}